#ifndef __COMMUNICATION_H_
#define __COMMUNICATION_H_



#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/stat.h>
#include <netinet/in.h> //sockeaddr_in
#include "log.h"
#include "ComLog.h"
#include <sys/select.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
#include <arpa/inet.h>
typedef enum eSOCKETTYPE_t
{
   SOCKET_CLIENT = 1,
   SOCKET_SERVER = 2,
   SOCKET_MAX

}eSOCKETTYPE;

typedef enum eSOCKETERROR_t
{
   SOCKETTRYAGAIN = 0,
   SOCKETDOWN = -1,
   SOCKETINVALID = -2,
   SOCKETERROR = -3,
   SOCKETTIMEOUT = -4,
   SOCKETMAX = -10
   
}eSOCKETERROR;


class CCommunication
{
   public :
      CCommunication();
      ~CCommunication();
      //bindaddr 为NULL 就是监听所有IP
      int SocketIntanceTCP(eSOCKETTYPE type, char * bindaddr, int bindport);
      int SocketIntanceUDP(eSOCKETTYPE type, char * bindaddr, int bindport);
      int SocketConnect(char *addrs, int port);
      int SocketListen(int maxcon);
      int SocketAccept(struct sockaddr_in *faraddr);
      int SocketWrite(char *data, int len);
      int SocketWrite(int fd , char *data, int len);
      int SocketRead(char *data, int len, int outtimems);
      int SocketRead(int fd, char *data, int len, int outtimems);
      int SocketClose(int fd);
      int SocketFdGet(); //获取创建得socket句柄
      int SocketGetAddrAndPort(struct sockaddr_in *sockaddr, char *addr, int addr_len, int &port);
   private:
      int socketfd;  
      eSOCKETTYPE sockettype;  
};


#endif